package xyz.lwm.lazycat.vertx;

import io.vertx.core.file.FileSystem;
import io.vertx.ext.web.FileUpload;
import xyz.lwm.lazycat.web.handle.Multipart;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * vertx upload file
 *
 * @author lwm
 */
class VertxUploadFile implements Multipart.File {

	private final FileUpload fileUpload;

	private final FileSystem fileSystem;

    VertxUploadFile(FileUpload fileUpload, FileSystem fileSystem) {
		this.fileUpload = fileUpload;
        this.fileSystem = fileSystem;
    }

    @Override
	public String getFileName() {
		return fileUpload.fileName();
	}

	@Override
	public long size() {
		return fileUpload.size();
	}

	@Override
	public boolean isUploaded() {
		return size() > 0;
	}

	@Override
	public byte[] getFileContent() throws IOException {
		return fileSystem.readFileBlocking(fileUpload.uploadedFileName()).getBytes();
	}

	@Override
	public InputStream getInputStream() throws IOException {
		return new ByteArrayInputStream(getFileContent());
	}

}
